package nix.mybatis;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import nix.mybatis.mapper.OrderMapper;
import nix.mybatis.mapper.UserMapper;
import nix.mybatis.pojo.Order;
import nix.mybatis.pojo.User;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

public class MyBatisMapperTest2 {
	
	@Test
	public void fun1() throws IOException {
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 通过SqlSession获得Mapper接口实现类
		OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
		// 执行sql
		List<Order> list = orderMapper.findListWithUser();
		for (Order order : list) {
			// 使用断点查看
			System.out.println(order);
		}
	}
	
	@Test
	public void fun2() throws IOException {
		// 加载核心配置文件
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		// 创建SqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		// 创建SqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		// 通过SqlSession获得Mapper接口实现类
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		// 执行sql
		List<User> list = userMapper.findListWithOrders();
		for (User user : list) {
			// 使用断点查看
			System.out.println(user);
		}		
	}
}
